package com.fasteat.mall.service.impl;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.fasteat.mall.entity.Product;
import com.fasteat.mall.entity.ProductComment;
import com.fasteat.mall.entity.vo.ProductCommentVo;
import com.fasteat.mall.mapper.ProductCommentMapper;
import com.fasteat.mall.mapper.ProductMapper;
import com.fasteat.mall.service.ProductCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasteat.mall.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hy
 * @since 2021-01-19
 */
@Service
public class ProductCommentServiceImpl extends ServiceImpl<ProductCommentMapper, ProductComment> implements ProductCommentService {

    @Autowired(required = false)
    private ProductCommentMapper productCommentMapper;

    @Autowired(required = false)
    private ProductMapper productService;

    @Override
    public List<ProductCommentVo> getUserCommentPage1(String userId, Integer limit, Integer size) {
        List<ProductComment> productComments = productCommentMapper.selectComment(userId, limit, size);
        List<ProductCommentVo> productCommentVos = new ArrayList<>();
        BeanUtils.copyProperties(productComments, productCommentVos);
        ProductCommentVo productCommentVo = new ProductCommentVo();
        List<String> productIds = productComments.stream().map(ProductComment :: getProductId).collect(Collectors.toList());
        List<Product> productLists = productService.selectBatchIds(productIds);
        Map<String, Product> productMap = productLists.stream().collect(Collectors.toMap(Product::getId, product -> product));
        for (ProductComment pc : productComments) {
            BeanUtils.copyProperties(pc, productCommentVo);
            BeanUtils.copyProperties(productMap.get(pc.getProductId()),productCommentVo);
        }
        return null;
    }

    @Override
    public List<ProductCommentVo> getUserCommentPage2(String userId, Integer limit, Integer size) {
        List<ProductCommentVo> vos = productCommentMapper.selectBySql(userId, limit, size);
        return vos;
    }
}
